import tkinter as tk
from tkinter import ttk
import tkinter.messagebox as msgbox
import pyperclip
from utils import formatter, each_len, fudong, en_size
from tkinter import font


def button_clicked():
    text = entry1.get("1.0", tk.END)
    try:
        line_len = int(entry2.get())
        fu = int(entry3.get())
        en_s = float(entry4.get())
    except Exception:
        line_len = None
        fu = None
        en_s = None
        entry2.delete(0, tk.END)
        entry3.delete(0, tk.END)
        entry4.delete(0, tk.END)
        entry2.insert(0, each_len)
        entry3.insert(0, fudong)
        entry4.insert(0, en_size)

    result_text.delete('1.0', tk.END)  # 清空右边大框的内容
    result_text.insert(tk.END, formatter(text, line_len, fu, en_s))


def clear_in_out():
    entry1.delete('1.0', tk.END)
    result_text.delete('1.0', tk.END)


def copy_res():
    text = result_text.get("1.0", tk.END)
    res = []
    res.append("\"\"\"")
    for str in text.split('\n'):
        if str == "" or str == " ":
            continue
        res.append("   " + str)
    res.append("\"\"\"")
    pyperclip.copy('\n'.join(res))
    msgbox.showinfo("提示", "已复制到剪贴板")


def copy_res1():
    text = result_text.get("1.0", tk.END)
    arr = text.split('\n')
    res = ''
    for str in arr:
        if str == '':
            continue
        str = '# ' + str
        res = res + str + '\n'
        res.rstrip('\n')

    pyperclip.copy(res)
    msgbox.showinfo("提示", "已复制到剪贴板")


# 创建主窗口
root = tk.Tk(className='格式化')

my_font = font.Font(family="Courier New", size=10, weight="bold", slant="italic")
# 获取屏幕的宽度和高度
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()

# 计算窗口的宽度和高度
window_width = int(screen_width * 1)
window_height = int(screen_height * 1)

# 设置窗口的大小和位置
root.geometry(
    f"{window_width}x{window_height}+{int((screen_width - window_width) / 2)}+{int((screen_height - window_height) / 2)}")

root.state('zoomed')

# 创建样式对象
style = ttk.Style()
style.configure("Placeholder.TEntry", foreground="gray")

top = tk.Label(root)
top.grid(row=0, column=0, sticky="nsew")

a = tk.Label(top)
b = tk.Label(top)
a.grid(row=0, column=0, sticky="nsew")
b.grid(row=0, column=1, sticky="nsew")
a.grid_rowconfigure(0, weight=1)
b.grid_rowconfigure(0, weight=1)

top.grid_rowconfigure(0, weight=1)
top.grid_columnconfigure(0, weight=1)
top.grid_columnconfigure(1, weight=1)

# 左边的大输入框
entry1 = tk.Text(a, font=my_font, width=60)
entry1.grid(row=0, column=0, sticky="nsew", padx=10, pady=5)

# 中间部分
middle_frame = tk.Frame(a)
middle_frame.grid(row=0, column=1, padx=10, pady=10)

# 每行长度
entry2 = ttk.Entry(middle_frame, style="Placeholder.TEntry")
entry2.insert(0, '每行长度')
entry2.grid(row=0, column=0, columnspan=2, pady=10)

# 波动范围
entry3 = ttk.Entry(middle_frame, style="Placeholder.TEntry")
entry3.insert(0, '波动范围')
entry3.grid(row=1, column=0, columnspan=2, pady=10)

# 字符串/汉字
entry4 = ttk.Entry(middle_frame, style="Placeholder.TEntry")
entry4.insert(0, '字符串/汉字')
entry4.grid(row=2, column=0, columnspan=2, pady=10)

button = tk.Button(middle_frame, text="生成", command=button_clicked)
button.grid(row=3, column=0, columnspan=1, pady=10, sticky="w")

button1 = tk.Button(middle_frame, text="清空", command=clear_in_out)
button1.grid(row=3, column=1, columnspan=1, pady=10, sticky="e")

copy_button = tk.Button(middle_frame, text="\"\"\"copy\"\"\"", command=copy_res)
copy_button.grid(row=4, column=1, columnspan=1, pady=10, sticky="e")
copy_button1 = tk.Button(middle_frame, text="#copy", command=copy_res1)
copy_button1.grid(row=5, column=1, columnspan=1, pady=10, sticky="e")

# 右边的大框
result_text = tk.Text(b, font=my_font, width=104)
result_text.grid(row=0, column=0, sticky="nsew", padx=10, pady=5)

# 设置root窗口的行高
root.grid_rowconfigure(0, weight=1)


# 绑定事件，当获取焦点时清空底部文字
def clear_placeholder_entry2(event):
    if entry2.get() == "每行长度":
        entry2.delete(0, tk.END)
        entry2.configure(foreground="black")


# 绑定事件，当失去焦点且内容为空时重新显示底部文字
def restore_placeholder_entry2(event):
    if not entry2.get():
        entry2.insert(0, "每行长度")
        entry2.configure(foreground="gray")


# 绑定事件，当获取焦点时清空底部文字
def clear_placeholder_entry3(event):
    if entry3.get() == "波动范围":
        entry3.delete(0, tk.END)
        entry3.configure(foreground="black")


# 绑定事件，当失去焦点且内容为空时重新显示底部文字
def restore_placeholder_entry3(event):
    if not entry3.get():
        entry3.insert(0, "波动范围")
        entry3.configure(foreground="gray")


# 绑定事件，当获取焦点时清空底部文字
def clear_placeholder_entry4(event):
    if entry4.get() == "字符串/汉字":
        entry4.delete(0, tk.END)
        entry4.configure(foreground="black")


# 绑定事件，当失去焦点且内容为空时重新显示底部文字
def restore_placeholder_entry4(event):
    if not entry4.get():
        entry4.insert(0, "字符串/汉字")
        entry4.configure(foreground="gray")


entry2.bind("<FocusIn>", clear_placeholder_entry2)
entry2.bind("<FocusOut>", restore_placeholder_entry2)
entry3.bind("<FocusIn>", clear_placeholder_entry3)
entry3.bind("<FocusOut>", restore_placeholder_entry3)
entry4.bind("<FocusIn>", clear_placeholder_entry4)
entry4.bind("<FocusOut>", restore_placeholder_entry4)

# 运行主循环
root.mainloop()
